module.exports=class extends baseController{
    constructor(){
        super();
        this.adminUserLogic=this.loadLogic('iframe/admin-user');
        this.router.post("/getItems", this.getItems.bind(this));
        this.router.post("/getItem", this.getItem.bind(this));
        this.router.post("/edit", this.edit.bind(this));
        this.router.post("/del", this.del.bind(this));
        this.router.post("/resetPwd", this.resetPwd.bind(this));
        return this.router;
    }

    static init(){
        return new this();
    }

    async getItems(req,res){
        let Result ={};
        try {
            let userInfo = req.user;
            let FindFilterModel = {};
            let curPage = req.body.CurPage ? Number(req.body.CurPage) : 1;
            let pageSize = req.body.PageSize ? Number(req.body.PageSize) : 20;
            let sort = req.body.Sort ? req.body.Sort : "UserID|desc";
            FindFilterModel.Query = req.body.Query;
            FindFilterModel.UserRealName = req.body.UserRealName;
            FindFilterModel.UserMobilePhone = req.body.UserMobilePhone;
            FindFilterModel.UserJobNum = req.body.UserJobNum;
            FindFilterModel.UserCreatetime = req.body.UserCreatetime;
            FindFilterModel.UserRule = req.body.UserRule;
            FindFilterModel.userIds=req.body.userIds?(""+req.body.userIds).split(','):[];
            FindFilterModel.PartnersID=req.body.PartnersID? Number(req.body.PartnersID) : 0;
            FindFilterModel.UserOrgStructID = req.body.UserOrgStructID ? req.body.UserOrgStructID : '';
            let Ret = await this.adminUserLogic.getItems(FindFilterModel,curPage,pageSize,sort,req.headers.moduleid,userInfo);
            Result=this.Success(1,Ret);
        } catch (err) {
            console.log(err);
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async getItem(req,res){
        var CB = {};
        try {
            var UserInfo = req.user;
            var uid = Number("0" + req.body.uid);
            var Result={};
            if (uid > 0) {
                let model = await this.adminUserLogic.getItem(uid);
                if (!model) {
                    CB=this.Error("记录不存在");
                    return res.json(CB);
                }else{
                    Result.model=JSON.parse(JSON.stringify(model));
                }
            }else{
                Result.model={
                    UserPartnersID:UserInfo.UserPartnersID
                };
            }

            if (UserInfo.IsShowAll){
                Result.isShowPartnersSelect=true;
            }else{
                Result.isShowPartnersSelect=false;
            }
            CB = this.Success(1,Result);
        } catch (err) {
            console.log(err);
            CB = this.Error('数据查询失败',err);
        }
        res.json(CB);
    }

    async edit(req,res){
        let CB = {};
        try {
            let model={};
            model.UserID=req.body.UserID ? Number(req.body.UserID) : 0;
            model.UserMobilePhone = req.body.UserMobilePhone;
            model.UserRealName = req.body.UserRealName;
            model.UserPingYin = this.webTools.pinyin.getWordsCode(model.UserRealName);;
            model.UserJobNum = req.body.UserJobNum;
            model.UserRule = req.body.UserRule;
            model.UserPartnersID=req.body.UserPartnersID || -1;
            model.UserOrgStructID = req.body.UserOrgStructID ||  model.UserPartnersID;

            model.UserAreaCode=req.body.UserAreaCode || '';
            model.UserType=req.body.UserType || '';
            model.UserHeaderImg=req.body.UserHeaderImg ;

            let ret = await this.adminUserLogic.insertOrUpdate(model);
            if(ret){
                CB = this.Success(1,ret);
            }else{
                CB = this.Error("记录不存在");
            }
        } catch (err) {
            CB = this.Error("数据更新失败",err);
        }
        res.json(CB);
    }

    async del(req,res){
        let CB ={};
        try {
            let keys = req.body.keys;
            await this.adminUserLogic.del(keys);
            CB=this.Success(1);
        } catch (err) {
            console.log(err);
            CB=this.Error("删除失败",err);
        }
        res.json(CB);
    }

    async resetPwd(req,res){
        let CB = {};
        try {
            let uid = req.body.uid;
            await this.adminUserLogic.resetPwd(uid);
            CB= this.Success(1);
        } catch (err) {
            CB= this.Error("重置密码失败",err);
            console.log(err);
        }
        res.json(CB);
    }

}
